// 人脸识别视图

var $pic1, $pic2, $pic3;
var $verifyBtn, $phone, $verification_code, $name, $frId;
var daojs = 60;
var p1t, p1l, p2t, p2l, p3t, p3l;
var demoH, demoW, initW;
var handlesm, zuiHandle;
var isMove = false; // 是否允许滑动
var p1d = 5,
    p2d = 50,
    p3d = 95;
var opacity1 = 1,
    opacity2 = 0,
    opacity3 = 0;
var parentW, leftVal;
var idRegExp;
/*
import{
    idRegExp
} from '../components/commonRegExp.js';*/
import {
    titileInputInit
} from '../components/titleInput.js';

function viewInit(argument) {
    initVal();
    titileInputInit();
    bindViewEvent();
    $("#fanhui").on("touchstart", function() {
        history.back();
    });

    mnsaomiao();
    $("body").on("swipeleft", function() {
        if (isMove) {
            movePic();
            setCheckedLi();
        }
    });
}

// 验证码倒计时
function djsfunc() {
    var handle = setInterval(function() {
        $verifyBtn.text("重新获取(" + daojs + "S)");
        daojs--;
        if (daojs == 0) {
            clearInterval(handle);
            $verifyBtn.attr("weui-btn weui-btn_plain-primary");
            $verifyBtn.text("重新获取验证码");
            daojs = 60;
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        }
    }, 1000);
}

// 验证输入框 是否允许提交
function checkedInputValue() {
    var nameVal = $name.val();
    var frIdVal = $frId.val();
    var nameLength = len(nameVal);
    var verifyCode = $verification_code.val();
    if ($phone.val().length == 11 && verifyCode.length == 4 && nameLength > 2 && idRegExp.test(frIdVal)) {
        $("#submit_btn").attr("class", "weui-btn weui-btn_primary");
    } else {
        $("#submit_btn").attr("class", "weui-btn weui-btn_disabled weui-btn_default ui-link");
    }
}

// 计算姓名长度
function getNameLength() {
    var nameVal = $name.val();
    var nameLength = len(nameVal);
    return nameLength;
}

//取字符串长度，中文算2位
function len(s) {
    var l = 0;
    var a = s.split("");
    for (var i = 0; i < a.length; i++) {
        if (a[i].charCodeAt(0) < 299) {
            l++;
        } else {
            l += 2;
        }
    }
    return l;
}

// 
function idInput() {
    var idVal = $frId.val();
    idVal = idVal.replace(/\D/g, "X")
    $frId.val(idVal);
}

// 

// 绑定事件
function bindViewEvent() {
    $("#phone").on("input", function() {
        var oldPhone = $(this).val();
        if (oldPhone.length == 11) {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-primary");
        } else {
            $verifyBtn.attr("class", "weui-btn weui-btn_plain-default weui-btn_plain-disabled");
        }
    });

    $(".weui-cells_form").on("input", checkedInputValue);
    $frId.on("input", idInput);
    $("#checkButton").on("click",function  () {
        var $agreeInput = $("#checkButton").find("input");
        if($agreeInput.attr("checked")){
            $(".icheckbox-blue").removeClass("checked");
            $agreeInput.attr("checked","");
            $agreeInput.removeAttr("checked");

        }else{
            $(".icheckbox-blue").addClass("checked");
            $agreeInput.attr("checked","");
        }
    });
    $("#contract").on("click",function  () {
        location.href="./contract.html";
    });
}

function initVal() {

    $pic1 = $(".demo_pic1");
    $pic2 = $(".demo_pic2");
    $pic3 = $(".demo_pic3");
    $verifyBtn = $("#verifyBtn");
    $phone = $("#phone");
    $verification_code = $("#verification_code");
    $name = $("#name");
    $frId = $("#frId");
    idRegExp = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/;

}

// 初始化第二步
function initStep2View() {
    $("#demo-box .demo-big>span").text($name.val().substr(1));
}

function setRoundLeft() {
    parentW = $("#demo-round").width();
    leftVal = (parentW / 2 - 800) + "px";
    $("#demo-round>div").css("left", leftVal);
}

// 设置初始位置
function setDemoPic() {
    demoH = $("#demo_png_box").height();
    demoW = $("#demo_png_box").width();
    initW = (demoW - 197) / 2;
    $(".demo_pic1").css({
        "left": initW
    });
    var p2w = demoH + "px";
    var p2h = demoH + "px";
    $(".demo_pic2").css({
        "top": p2w,
        "left": p2h
    });
}

// 移动函数
function movePic() {
    isMove = false;
    var interHandle = setInterval(function() {
        p1d--;
        p2d--;
        p3d--;
        p1t = (2 * demoH - 2 * demoH * Math.cos(p1d % 360 * 0.017453293)) + "px";
        p1l = (2 * demoH * Math.sin(p1d % 360 * 0.017453293)) + "px";
        p2t = (2 * demoH - 2 * demoH * Math.cos(p2d % 360 * 0.017453293)) + "px";
        p2l = (2 * demoH * Math.sin(p2d % 360 * 0.017453293));
        p3t = (2 * demoH - 2 * demoH * Math.cos(p3d % 360 * 0.017453293)) + "px";
        p3l = (2 * demoH * Math.sin(p3d % 360 * 0.017453293));
        $pic1.css({
            "top": p1t,
            "left": p1l
        });
        $pic2.css({
            "top": p2t,
            "left": p2l + "px"
        });
        $pic3.css({
            "top": p3t,
            "left": p3l + "px"
        });
        if (p2l - initW < 5 && p2l - initW > -5) {
            clearInterval(interHandle);
            clearInterval(handlesm);
            $("#step-box>div:nth-child(2)>.circle").addClass("chekced");

            opacity2 = 1;
            $pic2.css({
                "top": "0px",
                "left": initW + "px",
                "opacity": 1
            });
            var $zui = $("#zui_img");
            var ztime = 0;
            zuiHandle = setInterval(function() {
                ztime++;
                var zuiIndex = ztime % 3;
                $pic2.removeClass("demo_pic2_0");
                $pic2.removeClass("demo_pic2_1");
                $pic2.removeClass("demo_pic2_2");
                $pic2.addClass("demo_pic2_" + zuiIndex);
            }, 500);

            isMove = true;

        }
        if (p3l - initW < 5 && p3l - initW > -5) {
            clearInterval(zuiHandle);
            clearInterval(interHandle);
            opacity3 = 1;
            $("#step-box>div:nth-child(3)>.circle").addClass("chekced");
            $pic3.css({
                "top": "0px",
                "left": initW + "px",
                "opacity": 1
            });

            $("#button-box .nums").hide();
            $("#recording").css("display", "block");
        }
        setOpacity();
        console.log(p1d % 360, p1t, p1l);
        console.log(p2d % 360, p2t, p2l);
    }, 50)
}

// 设置选定的li
function setCheckedLi() {
    if ($(".nums li:eq(0)").hasClass("chekced")) {
        $(".nums .chekced").removeClass("chekced");
        $(".nums li:eq(1)").addClass("chekced");
    } else if ($(".nums li:eq(1)").hasClass("chekced")) {
        $(".nums .chekced").removeClass("chekced");
        $(".nums li:eq(2)").addClass("chekced");
    }
}

// 设置透明度
function setOpacity() {

    if (p2l - initW > 6) {
        opacity1 -= 0.05;
        opacity2 += 0.05;
    } else if (p2l - initW < 6) {
        opacity2 -= 0.05;
        opacity3 += 0.05;
    }
    $pic1.css("opacity", opacity1);
    $pic2.css("opacity", opacity2);
    $pic3.css("opacity", opacity3);
}

// 模拟扫码
function mnsaomiao() {
    var p1mtVal = 0;
    handlesm = setInterval(function() {
        p1mtVal += 5;
        $(".demo_pic1 img").css("top", p1mtVal + "%");
        if (p1mtVal == 100) {
            p1mtVal = 0;
        }
    }, 80);
    isMove = true;

}

// 设置验证结算
function setStepLoaderEnd(stepVal) {
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    var selector_duiguo = "#step3 .step_" + stepVal + " .duiguo_parent";
    $(selector_loader).hide();
    $(selector_duiguo).show();
}
// 正在验证查询第几步骤
function setStepLoader(stepVal) {
    var selector_wait = "#step3 .step_" + stepVal + " .step3_wait";
    var selector_loader = "#step3 .step_" + stepVal + " .loader-inner";
    $(selector_wait).hide();
    $(selector_loader).show();
}

export {
    viewInit,
    djsfunc,
    setDemoPic,
    setRoundLeft,
    setStepLoader,
    setStepLoaderEnd,
    initStep2View
}